﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKArch.AreaStatistical.DataManager
{
	public class AreaInfoManager
	{
		public static void WriteExtensibleStorageToArea(Area area, AreaType areaType, string suiteName, string hoomNumb, List<ElementId> roomIdList)
		{
			Guid guid = new Guid("82FEEA0A-95F2-4D07-AB7C-73E4EFFDA615");
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("AreaExtensibleInfo");
				schemaBuilder.AddSimpleField("AreaType", typeof(int));
				schemaBuilder.AddSimpleField("SuiteName", typeof(string));
				schemaBuilder.AddSimpleField("HoomNumb", typeof(string));
				schemaBuilder.AddArrayField("RoomIdList", typeof(int));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("AreaType");
			entity.Set<int>(field, (int)areaType);
			Field field2 = schema.GetField("SuiteName");
			entity.Set<string>(field2, suiteName);
			Field field3 = schema.GetField("HoomNumb");
			entity.Set<string>(field3, hoomNumb);
			Field field4 = schema.GetField("RoomIdList");
			IList<int> list = new List<int>();
			foreach (ElementId elementId in roomIdList)
			{
				list.Add(elementId.IntegerValue);
			}
			entity.Set<IList<int>>(field4, list);
			area.SetEntity(entity);
		}

		public static bool ReadExtensibleStorageFromArea(Area area, ref AreaType areaType, ref string suiteName, ref string hoomNumb, ref List<ElementId> roomIdList)
		{
			Schema schema = Schema.Lookup(new Guid("82FEEA0A-95F2-4D07-AB7C-73E4EFFDA615"));
			if (schema == null)
			{
				return false;
			}
			Entity entity = area.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			if (entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField("AreaType");
			areaType = (AreaType)entity.Get<int>(field);
			Field field2 = schema.GetField("SuiteName");
			suiteName = entity.Get<string>(field2);
			Field field3 = schema.GetField("HoomNumb");
			hoomNumb = entity.Get<string>(field3);
			Field field4 = schema.GetField("RoomIdList");
			foreach (int num in entity.Get<IList<int>>(field4))
			{
				roomIdList.Add(new ElementId(num));
			}
			return true;
		}

		public static bool HaxExtensibleStorageInArea(Area area, ref AreaType areaType)
		{
			areaType = AreaType.AT_None;
			Schema schema = Schema.Lookup(new Guid("82FEEA0A-95F2-4D07-AB7C-73E4EFFDA615"));
			if (schema == null)
			{
				return false;
			}
			Entity entity = area.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			if (entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField("AreaType");
			areaType = (AreaType)entity.Get<int>(field);
			return areaType != AreaType.AT_None;
		}

		public static bool AreaIsFireArea(Area area)
		{
			Schema schema = Schema.Lookup(new Guid("a07967b8-1c19-4700-bc62-a4c55ce08717"));
			return schema != null && schema.SchemaName.Equals("BoundaryElementId_XData");
		}

		private const string m_GUID = "82FEEA0A-95F2-4D07-AB7C-73E4EFFDA615";

		private const string m_SchemaName = "AreaExtensibleInfo";

		private const string m_AreaTypeFieldName = "AreaType";

		private const string m_SuiteNameFieldName = "SuiteName";

		private const string m_HoomNumbFieldName = "HoomNumb";

		private const string m_RoomIdListFieldName = "RoomIdList";
	}
}
